//
//  LGCiTangDetailViewController.m
//  ligenyun
//
//  Created by Mac on 2018/3/26.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGCiTangDetailViewController.h"
#import "LGCiTangRecordViewController.h"
#import "LGCiTangPhotoViewController.h"
#import "LGCiTangFamilyTreeViewController.h"
#import "LGCiTangJiSiViewController.h"
#import "LGCiTangGiveMoneyViewController.h"
#define buttonBackGroundColor  [[UIColor whiteColor] colorWithAlphaComponent:0.6]
#define buttonWith  50
@interface LGCiTangDetailViewController ()
@property(nonatomic, strong) STButton                     *goButton;
@property(nonatomic, strong) STButton                     *addButton;
@property(nonatomic, strong) UIImageView                     *bannerImageView;
@property(nonatomic, strong) NSMutableArray                     *menuButtonArray;
@end

@implementation LGCiTangDetailViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"赵氏祠堂";
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, [TMUtils navgationBarBootom], UIScreenWidth, [TMUtils tabBarTop] + 49 -  [TMUtils navgationBarBootom])];
    self.bannerImageView.image = [UIImage imageNamed:@"bg_祠堂"];
    self.bannerImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.bannerImageView];
    
    
    self.goButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonWith)
                                                     title:@"进入祠堂"
                                                titleColor:FirstTextColor
                                                 titleFont:14
                                              cornerRadius:25
                                           backgroundColor:buttonBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    self.goButton.titleLabel.numberOfLines = 0;
    self.goButton.left = 15;
    self.goButton.top = 20;
    self.goButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.bannerImageView addSubview:self.goButton];
    
    
    self.addButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonWith)
                                              title:@""
                                         titleColor:FirstTextColor
                                          titleFont:14
                                       cornerRadius:25
                                    backgroundColor:buttonBackGroundColor
                                    backgroundImage:nil
                                              image:[UIImage imageNamed:@"icon_添加"]];
    [self.addButton setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateSelected];
    self.addButton.titleLabel.numberOfLines = 0;
    self.addButton.right = UIScreenWidth -  15;
    self.addButton.bottom = self.bannerImageView.height - 70;
    self.addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.bannerImageView addSubview:self.addButton];
    [self.addButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedAddButton:sender];
    }];
}
#pragma mark --Private Method
- (void)showMenuButton{
    self.menuButtonArray = [NSMutableArray new];
  //  NSArray * titles = @[@"相册",@"档案",@"族谱",@"祭祀",@"捐赠"];
    STButton * xiangceButton = [self buttonWithTitle:@"相册"];
    STButton * danganButton = [self buttonWithTitle:@"档案"];
    STButton * zupuButton = [self buttonWithTitle:@"族谱"];
    STButton * jisiButton = [self buttonWithTitle:@"祭祀"];
    STButton * juanzengButton = [self buttonWithTitle:@"捐赠"];
    
    CGFloat juanzengButtonToBootom = self.addButton.top - 10;
    CGFloat jisiButtonToBootom = juanzengButtonToBootom - 10 - buttonWith;
    CGFloat zupuButtonToBootom = jisiButtonToBootom - 10 - buttonWith;
    CGFloat danganButtonToBootom = zupuButtonToBootom - 10 - buttonWith;
    CGFloat xiangceButtonToBootom = danganButtonToBootom - 10 - buttonWith;
    NSArray *  bootomArray = @[@(xiangceButtonToBootom),@(danganButtonToBootom),@(zupuButtonToBootom),@(jisiButtonToBootom),@(juanzengButtonToBootom)];
    [self.menuButtonArray addObject:danganButton];
    [self.menuButtonArray addObject:xiangceButton];
    [self.menuButtonArray addObject:zupuButton];
    [self.menuButtonArray addObject:jisiButton];
    [self.menuButtonArray addObject:juanzengButton];
    
    for (NSInteger i =0;i < self.menuButtonArray.count;i++) {
        NSNumber *bootom = bootomArray[i];
        STButton *button = self.menuButtonArray[i];
        CGFloat damping = (arc4random()%100) * 0.001;//0~0.1
        CGFloat velo = (arc4random()%100) * 0.001;//0~0.1
        CGFloat dirung = (arc4random()%250) * 0.001;//0~0.25
        [UIView animateWithDuration:0.25 + dirung  delay:0 usingSpringWithDamping:0.7+damping   initialSpringVelocity:0.65 - velo options:UIViewAnimationOptionTransitionNone animations:^{
            
            button.bottom =  bootom.floatValue;
            
        } completion:^(BOOL finished) {
            
        }];
    }

    
    
}
- (void)hideMenuButton{
    for (STButton * button in self.menuButtonArray) {
        [UIView animateWithDuration:0.25 animations:^{
            button.alpha = 0;
        } completion:^(BOOL finished) {
            [button removeFromSuperview];
        }];
    }
}
- (STButton*)buttonWithTitle:(NSString*)title{
    __weak typeof(self) weakSelf =  self;
    STButton* button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonWith)
                                              title:title
                                         titleColor:SecendTextColor
                                          titleFont:14
                                       cornerRadius:25
                                    backgroundColor:buttonBackGroundColor
                                    backgroundImage:nil
                                              image:nil];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedMenuButtonWithTitle:title];
    }];
    button.titleLabel.numberOfLines = 0;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.bannerImageView addSubview:button];
    button.frame = self.addButton.frame;
    return button;
}
#pragma mark --Action Method
- (void)onSelctedAddButton:(UIButton*)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self showMenuButton];
    }else{
        [self hideMenuButton];
    }
}
- (void)onSelctedMenuButtonWithTitle:(NSString*)title{
    if ([title isEqualToString:@"档案"]) {
        [self.navigationController pushViewController:[LGCiTangRecordViewController new] animated:YES];
    }
    if ([title isEqualToString:@"相册"]) {
         [self.navigationController pushViewController:[LGCiTangPhotoViewController new] animated:YES];
        
    }
    if ([title isEqualToString:@"族谱"]) {
         [self.navigationController pushViewController:[LGCiTangFamilyTreeViewController new] animated:YES];
        
    }
    if ([title isEqualToString:@"祭祀"]) {
        
         [self.navigationController pushViewController:[LGCiTangJiSiViewController new] animated:YES];
    }
    if ([title isEqualToString:@"捐赠"]) {
        
         [self.navigationController pushViewController:[LGCiTangGiveMoneyViewController new] animated:YES];
    }
}
@end
